<script setup>
import { ref } from 'vue'
import { useSellerStore } from '@/stores/sellerStore';
import { sellerRegisterAPI } from '@/apis/seller';
import router from '@/router';
import { User, Lock } from '@element-plus/icons-vue'
const sellerStore = useSellerStore()

//控制注册与登录表单的显示， 默认显示注册
const isRegister = ref(false)

const registerData = ref(
    {
        username: '',
        password: '',
        rePassword: ''
    }
)

const loginData = ref(
    {
        username: '',
        password: ''
    }
)
//校验二次输入密码的函数
const checkRePassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次输入密码'))
    } else if (value !== registerData.value.password) {
        callback(new Error('两次输入密码不一致'))
    } else {
        callback()
    }
}
//定义表单校验规则
const rules = {
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
        { min: 5, max: 16, message: '请输入长度5~16的字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { min: 5, max: 16, message: '请输入长度5~16的字符', trigger: 'blur' }
    ],
    rePassword: [
        //checkRePassword函数需要写道rules上面否则不能用
        { validator: checkRePassword, trigger: 'blur' }
    ]
}
//获取form实例做统一校验
const form = ref(null)

const doRegister = () => {
    //解构赋值
    const { username, password } = registerData.value
    //调用实例方法
    form.value.validate(async (valid) => {
        //valid:所有表单项都通过校验才为true
        if (valid) {
            const res = await sellerRegisterAPI({ username, password })
            console.log(res);
            if (res.data.code === 0) {
                //注册成功
                registerData.value.username = ''
                registerData.value.password = ''
                registerData.value.rePassword = ''
                loginData.value.username = ''
                loginData.value.password = ''
                isRegister.value = false
                ElMessage.success(res.data.msg ? res.data.msg : '注册成功')
            } else {
                //失败
                ElMessage.error('注册失败')
            }
        }
    }
    )
}

const doLogin = () => {
    //解构赋值
    const { username, password } = loginData.value
    //调用实例方法
    form.value.validate(async (valid) => {
        //valid:所以表单项都通过校验才为true
        if (valid) {
            await sellerStore.getSellerInfo({ username, password })
            if (sellerStore.sellerInfo.code === 0) {
                //登录成功
                ElMessage.success(sellerStore.sellerInfo.message ? sellerStore.sellerInfo.message : '登录成功')
                router.push('/seller')
            } else {
                //失败
                ElMessage.error(sellerStore.sellerInfo.message ? sellerStore.sellerInfo.message : '登录失败')
            }
        }
    }
    )
}


</script>

<template>
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            <!-- 注册表单 -->
            <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="registerData" :rules="rules"
                status-icon :hide-required-asterisk="true">
                <el-form-item>
                    <h1>注册</h1>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
                </el-form-item>
                <el-form-item prop="password"> 
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
                </el-form-item>
                <el-form-item prop="rePassword">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码" v-model="registerData.rePassword"></el-input>
                </el-form-item>
                <!-- 注册按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="doRegister">
                        注册
                    </el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isRegister = false">
                        ← 返回
                    </el-link>
                </el-form-item>
            </el-form>
            <!-- 登录表单 -->
            <el-form ref="form" size="large" :model="loginData" :rules="rules" autocomplete="off"
                :hide-required-asterisk="true" status-icon v-else>
                <el-form-item>
                    <h1>卖家登录</h1>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="loginData.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input name="password" :show-password="true" :prefix-icon="Lock" type="password"
                        placeholder="请输入密码" v-model="loginData.password"></el-input>
                </el-form-item>
                <el-form-item class="flex">
                    <div class="flex">
                        <el-checkbox>我已同意隐私条款和服务条款</el-checkbox>
                        <el-link type="primary" :underline="false">忘记密码？</el-link>
                    </div>
                </el-form-item>
                <!-- 登录按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="doLogin">登录</el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isRegister = true">
                        注册 →
                    </el-link>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<style lang="scss" scoped>
/* 样式 */
.login-page {
    height: 100vh;
    background-color: #fff;

    .bg {
        background: url('@/assets/images/big-logo.jpg') no-repeat center / cover;
        border-radius: 0 20px 20px 0;
    }

    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;

        .title {
            margin: 0 auto;
        }

        .button {
            width: 100%;
        }

        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
}
</style>